#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
#include <chrono>
#include <vector>
#include "Util/LocalTime.h"

// 格式化时间为字符串
std::string formatTime(const struct tm& time) {
    char buffer[80];
    strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &time);
    return std::string(buffer);
}

// 测试noLocksLocaltime函数
void testNoLocksLocaltime() {
    std::cout << "测试 noLocksLocaltime 函数：" << std::endl;
    
    // 获取当前时间
    time_t now = time(nullptr);
    
    // 使用标准localtime
    struct tm stdTime;
#ifdef _WIN32
    localtime_s(&stdTime, &now);
#else
    localtime_r(&now, &stdTime);
#endif
    
    // 使用我们的noLocksLocaltime
    struct tm customTime;
    toolkit::noLocksLocaltime(&customTime, now);
    
    // 输出比较结果
    std::cout << "标准localtime结果: " << formatTime(stdTime) << std::endl;
    std::cout << "noLocksLocaltime结果: " << formatTime(customTime) << std::endl;
    
    // 详细比较各个字段
    std::cout << "\n详细比较：" << std::endl;
    std::cout << std::setw(15) << "字段" << std::setw(15) << "标准" << std::setw(15) << "自定义" << std::endl;
    std::cout << std::string(45, '-') << std::endl;
    std::cout << std::setw(15) << "年" << std::setw(15) << 1900 + stdTime.tm_year << std::setw(15) << 1900 + customTime.tm_year << std::endl;
    std::cout << std::setw(15) << "月" << std::setw(15) << stdTime.tm_mon + 1 << std::setw(15) << customTime.tm_mon + 1 << std::endl;
    std::cout << std::setw(15) << "日" << std::setw(15) << stdTime.tm_mday << std::setw(15) << customTime.tm_mday << std::endl;
    std::cout << std::setw(15) << "时" << std::setw(15) << stdTime.tm_hour << std::setw(15) << customTime.tm_hour << std::endl;
    std::cout << std::setw(15) << "分" << std::setw(15) << stdTime.tm_min << std::setw(15) << customTime.tm_min << std::endl;
    std::cout << std::setw(15) << "秒" << std::setw(15) << stdTime.tm_sec << std::setw(15) << customTime.tm_sec << std::endl;
    std::cout << std::setw(15) << "星期" << std::setw(15) << stdTime.tm_wday << std::setw(15) << customTime.tm_wday << std::endl;
    std::cout << std::setw(15) << "年日" << std::setw(15) << stdTime.tm_yday << std::setw(15) << customTime.tm_yday << std::endl;
    std::cout << std::setw(15) << "夏令时" << std::setw(15) << stdTime.tm_isdst << std::setw(15) << customTime.tm_isdst << std::endl;
}

// 测试不同时间点
void testDifferentTimePoints() {
    std::cout << "\n测试不同时间点：" << std::endl;
    
    // 测试时间点列表（Unix时间戳）
    std::vector<time_t> timePoints = {
        0,                      // 1970-01-01 00:00:00
        60 * 60 * 24 * 365,     // 大约1971年
        946684800,              // 2000-01-01 00:00:00
        1609459200,             // 2021-01-01 00:00:00
        time(nullptr)           // 当前时间
    };
    
    for (const auto& timePoint : timePoints) {
        // 使用标准localtime
        struct tm stdTime;
#ifdef _WIN32
        localtime_s(&stdTime, &timePoint);
#else
        localtime_r(&timePoint, &stdTime);
#endif
        
        // 使用我们的noLocksLocaltime
        struct tm customTime;
        toolkit::noLocksLocaltime(&customTime, timePoint);
        
        std::cout << "时间戳: " << timePoint << std::endl;
        std::cout << "标准localtime: " << formatTime(stdTime) << std::endl;
        std::cout << "noLocksLocaltime: " << formatTime(customTime) << std::endl;
        std::cout << std::string(50, '-') << std::endl;
    }
}

// 测试性能比较
void testPerformance() {
    std::cout << "\n性能测试：" << std::endl;
    
    const int iterations = 1000000;
    time_t now = time(nullptr);
    
    // 测试标准localtime
    auto startStd = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < iterations; ++i) {
        struct tm stdTime;
#ifdef _WIN32
        localtime_s(&stdTime, &now);
#else
        localtime_r(&now, &stdTime);
#endif
    }
    auto endStd = std::chrono::high_resolution_clock::now();
    auto durationStd = std::chrono::duration_cast<std::chrono::milliseconds>(endStd - startStd);
    
    // 测试noLocksLocaltime
    auto startCustom = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < iterations; ++i) {
        struct tm customTime;
        toolkit::noLocksLocaltime(&customTime, now);
    }
    auto endCustom = std::chrono::high_resolution_clock::now();
    auto durationCustom = std::chrono::duration_cast<std::chrono::milliseconds>(endCustom - startCustom);
    
    std::cout << "标准localtime执行 " << iterations << " 次耗时: " 
              << durationStd.count() << " 毫秒" << std::endl;
    std::cout << "noLocksLocaltime执行 " << iterations << " 次耗时: " 
              << durationCustom.count() << " 毫秒" << std::endl;
    
    double speedup = static_cast<double>(durationStd.count()) / durationCustom.count();
    std::cout << "性能比率: " << speedup << "x" 
              << (speedup > 1.0 ? " (noLocksLocaltime更快)" : " (标准localtime更快)") << std::endl;
}

int main() {
    // 初始化本地时间设置
    toolkit::localTimeInit();
    
    std::cout << "===== 本地时间函数测试 =====" << std::endl;
    
    // 输出当前时区和夏令时信息
    std::cout << "当前夏令时状态: " << (toolkit::getDaylightActive() ? "激活" : "未激活") << std::endl;
    
    // 运行测试
    testNoLocksLocaltime();
    testDifferentTimePoints();
    testPerformance();
    
    return 0;
}